<template>
	<div class="login_background">
		<div class="login_background_front"></div>
		<div class="login_main">
			<div class="login-form">
				<a-card>
					<div class="login-header">
						<h2>三方登录</h2>
					</div>
					<a-spin tip="正在登录中...">
						<div class="h-[300px]">
							<a-skeleton />
						</div>
					</a-spin>
				</a-card>
			</div>
		</div>
	</div>
</template>

<script setup name="loginCallback">
	import { message } from 'ant-design-vue'
	import tool from '@/utils/tool'
	import router from '@/router'
	import thirdApi from '@/api/auth/thirdApi'
	import loginApi from '@/api/auth/loginApi'
	import userCenterApi from '@/api/sys/userCenterApi'
	import dictApi from '@/api/dev/dictApi'
	import { globalStore, iframeStore, keepAliveStore, viewTagsStore } from '@/store'
	import configData from '@/config'
	import configApi from '@/api/dev/configApi'
	import { afterLogin } from '@/views/auth/login/util'

	const store = globalStore()
	const sysBaseConfig = computed(() => {
		return store.sysBaseConfig
	})

	const kStore = keepAliveStore()
	const iStore = iframeStore()
	const vStore = viewTagsStore()

	const setSysBaseConfig = store.setSysBaseConfig
	const clearKeepLive = kStore.clearKeepLive
	const clearIframeList = iStore.clearIframeList
	const clearViewTags = vStore.clearViewTags

	onBeforeMount(() => {
		clearViewTags()
		clearKeepLive()
		clearIframeList()
	})

	onMounted(() => {
		// 获取当前url
		const url = new URL(window.location.href)
		const params = {}
		let isHasCode = false

		url.searchParams.forEach((value, key) => {
			params[key] = value
			if (key === 'code') {
				isHasCode = true
			}
		})

		// 必须要有code参数
		if (!isHasCode) {
			message.error('code参数不存在')
			window.location.href = '/login'
			return
		}

		// 判断是否为SCNU SSO登录
		if (url.hostname.includes('scnu.edu.cn') || url.hostname.includes('soulmeet.scnu.edu.cn')) {
			// 加载系统配置
			let formData = ref(configData.SYS_BASE_CONFIG)
			configApi
				.configSysBaseList()
				.then((data) => {
					if (data) {
						data.forEach((item) => {
							formData.value[item.configKey] = item.configValue
						})
						tool.data.set('SNOWY_SYS_BASE_CONFIG', formData.value)
						setSysBaseConfig(formData.value)
					}
				})
				.catch(() => {})

			// 调用SSO登录接口
			loginApi
				.ssoLoginAuth(params)
				.then((loginToken) => {
					afterLogin(loginToken)
				})
				.catch((error) => {
					message.error('华南师范大学统一认证登录失败: ' + (error.message || '未知错误'))
					window.location.href = '/login'
				})
		} else {
			message.info('三方登录')

			thirdApi
				.thirdCallback(params)
				.then((data) => {
					tool.data.set('TOKEN', data)
					// 获取登录的用户信息
					loginApi.getLoginUser().then((loginUser) => {
						tool.data.set('USER_INFO', loginUser)
					})
					userCenterApi.userLoginMenu().then((menu) => {
						const indexMenu = menu[0].children[0].path
						tool.data.set('MENU', menu)
						// 重置系统默认应用
						tool.data.set('SNOWY_MENU_MODULE_ID', menu[0].id)
						router.replace({
							path: indexMenu
						})
						message.success('登录成功')
						dictApi.dictTree().then((dictData) => {
							// 设置字典到store中
							tool.data.set('DICT_TYPE_TREE_DATA', dictData)
						})
					})
				})
				.catch((error) => {
					message.error('第三方登录失败: ' + (error.message || '未知错误'))
					// window.location.href = '/login'
				})
		}
	})
	// logo链接
	const handleLink = (e) => {
		if (!sysBaseConfig.value.SNOWY_SYS_COPYRIGHT_URL) {
			e?.stopPropagation()
			e?.preventDefault()
		}
	}
</script>

<style lang="less" scoped>
	@import 'login';
</style>
